<?php

/**
 * item actions.
 *
 * @package    his
 * @subpackage item
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class itemActions extends sfActions
{

  public function executeSearch(sfWebRequest $request)
  {
    $this->forwardUnless($query = $request->getParameter('query'), 'item', 'index');
 
    $this->items = Doctrine_Core::getTable('HisItem') ->getForLuceneQuery($query);
  }
  
  
  public function executeIndex(sfWebRequest $request)
  {
   $this->categories = Doctrine_Core::getTable('HisCategory')->getWithItems();
   
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->item = $this->getRoute()->getObject();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new HisItemForm();
  }

  public function executeCreate(sfWebRequest $request)
  {

    $this->form = new HisItemForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    
    $this->form = new HisItemForm($this->getRoute()->getObject());
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->form = new HisItemForm($this->getRoute()->getObject());

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();
    
    $item = $this->getRoute()->getObject();
    $item->delete();

    $this->redirect('item/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
     $form->bind(
    $request->getParameter($form->getName()),
    $request->getFiles($form->getName())
  );
 
  if ($form->isValid())
  {
    $item = $form->save();
 
    $this->redirect('item_show', $item);
  }    
  }
  
  public function executePublish(sfWebRequest $request)
{
  $request->checkCSRFProtection();
 
  $item = $this->getRoute()->getObject();
  $item->publish();
 
  $this->getUser()->setFlash('notice', sprintf('Your item is now online.'));
 
  $this->redirect('item_show_user', $item);
}
}
